﻿Shader "Dan/UI_ArrowMove01"
{
    Properties
    {
        _Color ("Color", Color) = (0.1,0.4,0.8,1)
        _Color2 ("Color2", Color) = (1,0,0,1)
        _Emission ("Emission", Float ) = 1
        _Level ("Level", Float ) = 4
        _Width ("Width", Range(0, 1)) = 0
        _Percentage ("Percentage", Range(0, 1)) = 0
        [MaterialToggle] _UseTime ("UseTime", Float ) = 0
        _TimeSpeed ("TimeSpeed", Float ) = 0.5
        _BG_Opacity ("BG_Opacity", Range(0, 1)) = 0.1
        [HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    }
    SubShader
    {
        Tags { "IgnoreProjector"="True"
                "Queue"="Transparent"
               "RenderType"="Transparent" }
        LOD 100

        Pass
        {
            //Tags { "LightMode"="ForwardBase" }
            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off    
           
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            float4 _Color;
            float _TimeSpeed;
            float _Level;
            float _Width;
            fixed _UseTime;
            float _Percentage;
            float4 _Color2;
            float _Emission;
            float _BG_Opacity;
           
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv; 
                return o;
            }

            float DrawArrow(float2 uv, float width, float amount){
                float size =width*0.5+0.5;
                float val =frac(saturate(abs(uv.y-0.5)+frac(uv.x*amount))); 
                return step(size,val);    
            }

            fixed4 frag (v2f i) : SV_Target
            {
                float2 uv =i.uv;//主UV
                float stepX = uv.x*_Level;//分层
                float arrowShape = DrawArrow(uv,_Width,_Level);//画箭头
                
                //闪烁
                float time01 = min(0.9999,_Percentage);
                float time02 = frac(_Time.y*_TimeSpeed);
                float time =lerp(time01, time02 , _UseTime )*_Level;
                float flicker =floor(fmod(time,_Level));
                
                //单片流动
                float flow = stepX-flicker;       
                float m1 =floor(flow)-0.5;
                float m2 =ceil(flow)-0.5;
                float mask =saturate(0.5-m1*m2*2);
                
                
                //流动箭头
                float finalArrow = saturate(arrowShape*mask);               
                //发光
                float emissionVal =saturate(ceil(stepX)/_Level)*_Emission;//ceil取整
                //颜色渐变
                float3 finalColor = lerp(_Color.rgb,_Color2.rgb,emissionVal);
                return fixed4(finalColor,(finalArrow+(arrowShape*(1.0 - finalArrow)*_BG_Opacity)));
                
            }
            ENDCG
        }
    }
}
